// 已平台发货
var tbcnItem;
// 高级检索条件
var searchCondition = {
    "shopId" : ""

};

$(document).ready(function() {
    $('.panel-default .datetimepicker-default').datetimepicker();

    // 构建店铺下拉列表
    $.ajax({
        type : "GET",
        url : "/shop/unpagedShopList.action?prjId=" + window.top.window.$("#projectSwitch").val(),
        contentType : "application/json;charset=utf-8",
        dataType : "json",
        async : true,
        success : function(data) {
            if (data.result == "success") {
                $("#shopId option").remove();
                $("#shopId").append("<option value=''>请选择</option>");
                $.each(data.data, function(n, value) {
                    $('#shopId').append("<option value='" + value.shopId + "'>" + value.shopName + "</option>");
                });
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error : function(XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });


     getConsigned();

});

// 设置高级查询条件
function setSearchCondition() {
    var name=$("#shopId option:selected").text();
    if("请选择" == name){
        alert("请选择网店");
        return;
    }
    // 设置高级查询条件
    searchCondition = {
        "shopId" : encodeURI($('#shopId').val())

    };
}

// 快速查询回车
$('#quickSearch').keydown(function(e) {
    if (e.keyCode == 13 && $('#btnQuickSearch').is(":visible")) {
        $('#btnQuickSearch').click();
    }
});

// 快速查询按钮
$("#btnQuickSearch").click(function() {
    var name=$("#shopId option:selected").text();
    if("请选择" == name){
        alert("请选择网店");
        return;
    }
    setSearchCondition();
    drawTable();
});

// 高级查询按钮
$("#advSearchSubmit").click(function() {
    var name=$("#shopId option:selected").text();
    if("请选择" == name){
        alert("请选择网店");
        return;
    }
    setHidValue("frmAdvSearch");
    setSearchCondition();
    drawTable();
});

// 重置按钮
$("#advSearchReset").click(function() {
});

// 隐藏高级查询
$("#advSearchPanel").hide();
$("#btnAdvSearch").click(function() {
    if ($("#advSearchPanel").is(":hidden")) {
        $("#advSearchPanel").show();
        $("#btnQuickSearch").hide();
        $(this).children("i:eq(0)").removeClass("fa-angle-double-down");
        $(this).children("i:eq(0)").addClass("fa-angle-double-up");
    } else {
        $("#advSearchPanel").hide();
        $("#btnQuickSearch").show();
        $(this).children("i:eq(0)").removeClass("fa-angle-double-up");
        $(this).children("i:eq(0)").addClass("fa-angle-double-down");
    }
});


// 菜鸟商品列表
function getConsigned() {


    tbcnItem = $('#tbcnItem').DataTable(
        {
            "processing" : true,
            "serverSide" : true,
            "lengthMenu" : [ [ 10, 25, 50, 100, 500, 1000 ], [ 10, 25, 50, 100, 500, 1000 ] ],
            "dom" : '<"bottom"rtflp>',
            "searching" : false,
            "scrollX" : true,
            "scrollCollapse" : true,
            "pagingType" : "full_numbers",
            "deferRender" : true,
            "ajax" : {
                "url" :"/cainiaoitem/cainiaogetItemList.action",
                "data" : function(d) {
                    d.quickSearch = encodeURI($('#quickSearch').val());
                    d.formJson = JSON.stringify(searchCondition);
                }
            },
            "tableTools": {
                //"sSwfPath": "/js/datatables/tabletools/swf/copy_csv_xls_pdf.swf",
                //"aButtons": [],
                "sRowSelect": "bootstrap"
            },
            "columns": [
                {"data": "itemId", "searchable": false},
                {"data": "itemCode"},
                {"data": "shopAccount"},
                {"data": "title"},
                {"data": "type"},
                {"data": "skuCode"},
                {"data": "skuName"},
                {"data": "goodsCode"},
                {"data": "goodsName"}
            ],
            "columnDefs" : [{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 0 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 1 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 2 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 3 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 4 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 5 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 6 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 7 ]
            },{
                "className" : "truncate-text tooltip-column-detail text-left",
                "targets" : [ 8 ]
            }],
            "order": [[ 0, "asc" ]]
        });
    // 文字过长截断显示悬浮框
    $("#tbcnItem").tooltip({
        selector : ".tooltip-column-detail",
        container : "body",
        title : function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });

}



//刷新高级查询和快速查询
function refreshCheck() {
    $('#quickSearch').val("");
    $("#advSearchReset").click();
    searchCondition = {};
    if (!$("#advSearchPanel").is(":hidden")) {
        $("#btnAdvSearch").click();
    }
}

//重画表格
function drawTable() {
    tbcnItem.draw();
}